package com.mason.forum.controller;

import com.mason.forum.common.APPResult;
import com.mason.forum.common.ResultCode;
import com.mason.forum.model.ArticleReply;
import com.mason.forum.model.User;
import com.mason.forum.model.vo.ArticleReplyVO;
import com.mason.forum.service.IArticleReplyService;
import com.mason.forum.utils.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-07
 * Time: 22:42
 */
@Api(tags = "帖子回复相关")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService articleReplyService;

    @ApiOperation(("回复帖子"))
    @PostMapping("/create")
    public APPResult replyArticle(HttpServletRequest request,
                                  @RequestParam("articleId") @ApiParam("帖子id") @NonNull Long articleId,
                                  @RequestParam("content") @ApiParam("回复内容") @NonNull String content) {
        // 1. 判断当前登录用户是否有权限
        User user = SessionUtils.getUser(request);
        if (user.getState() == 1 || user.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return APPResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 2. 构造初步的回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        // 3. service - 回复帖子
        articleReplyService.replyArticle(articleReply);
        // 4. 返回成功
        return APPResult.success();
    }

    @ApiOperation(("获取帖子的回复列表"))
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public APPResult replyList(@RequestParam("articleId") @ApiParam("帖子id") @NonNull Long articleId){
        List<ArticleReplyVO> articleReplyVOS = articleReplyService.selectRepliesById(articleId);
        if(articleReplyVOS == null) {
            articleReplyVOS = new ArrayList<>();
        }
        //直接返回
        return APPResult.success(articleReplyVOS);
    }
}
